package com.atguigu.rabitmq.consumer.listener04;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * 消息者限流
 *
 * @author Taeyang
 * @date 2025/4/8
 */
@Component
public class ConsumerLimit {
    // 暂时屏蔽 @RabbitListener(queues = "direct_queue_01")
    public void consumer01(Message message, Channel channel) throws IOException, InterruptedException {

        //处理消息慢一点
        TimeUnit.MILLISECONDS.sleep(500);

        byte[] body = message.getBody();
        String msg = new String(body);
        System.out.println("direct_queue_01 ...msg ----> " + msg + " " + new Date());

        // deliveryTag: 是消息的唯一标识符，在消费者处理消息时，用来确认具体哪条消息被处理。
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        // 确认已经消费
        channel.basicAck(deliveryTag, true);

    }
}
